package com.jw.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.response.Result;
import com.jw.system.entity.Goods;
import com.jw.system.entity.User;
import com.jw.system.entity.vo.ContractVO;
import com.jw.system.entity.vo.GoodsVO;
import com.jw.system.entity.vo.UserVO;
import com.jw.system.service.ContractService;
import com.jw.system.service.CustomerService;
import com.jw.system.service.GoodsService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Contact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.ParseException;

/**
 * <p>
 * 合同表 前端控制器
 * </p>
 *
 * @author jw
 * @since 2021-04-20
 */
@RestController
@RequestMapping("/con")
public class ContractController {

    @Autowired
    private ContractService contractService;

    @Autowired
    private GoodsController goodsController;

    /**
     * 获取合同列表
     *
     * @return
     */
    @ApiOperation(value = "合同列表", notes = "模糊查询合同列表")
    @PostMapping("/getConList")
    public Result getConList(@RequestParam(value = "current", defaultValue = "1") Integer current,
                             @RequestParam(value = "size", defaultValue = "7") Integer size,
                             @RequestParam(value = "audit", required = false) int audit,
                             @RequestBody ContractVO contractVO) {
        Page<ContractVO> consList = contractService.getConList(current, size, audit,contractVO,-1L,-1);
        return Result.ok().data("consList", consList.getRecords()).data("total", consList.getTotal());
    }

    /**
     * 合同订单明细
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "合同信息", notes = "id查询合同信息")
    @GetMapping("/getConById")
    public Result getConById(@RequestParam(value = "id") Long id) {
        ContractVO contractVO = contractService.getConById(id);
        return Result.ok().data("contract", contractVO);
    }

    /**
     * 合同订单明细
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "合同商品明细")
    @GetMapping("/getDetail")
    public Result detail(@RequestParam(value = "id") Long id,
                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                         @RequestParam(value = "categorys", required = false) String categorys,
                         @RequestParam(value = "name", required = false) String name) {
        Goods goods = new Goods();
        goods.setName(name);
        goodsController.buildCategorySearch(categorys, goods);
        Page<GoodsVO> goodsList = contractService.getDetail(pageNum, pageSize, id, goods);
        return Result.ok().data("goods", goodsList.getRecords()).data("total", goodsList.getTotal());
    }

    /**
     * 合同文件上传
     */
    @ApiOperation(value = "上传合同文件")
    @PostMapping("/upload")
    public Result detail(@RequestParam("id") Long id, MultipartFile file) {
        contractService.upload(id, file);
        return Result.ok();
    }

    /**
     * 添加合同
     */
    @ApiOperation(value = "添加合同")
    @PostMapping("/add")
    public Result add(@RequestBody ContractVO contractVO){
        System.out.println("contractVOcontractVOcontractVOcontractVOcontractVOcontractVO");
        System.out.println(contractVO);
        try {
            contractService.add(contractVO);
        }catch (ParseException e){
        }
        return Result.ok();
    }

    /**
     * 合同审核
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "入库审核")
    @GetMapping("/getApprove")
    public Result getApprove(@RequestParam(value = "id") Long id) {
        contractService.getApprove(id);
        return Result.ok();
    }

}

